package cn.xny.org.service.impl;

import cn.xny.basic.service.BaseService;
import cn.xny.basic.service.impl.BaseServiceImpl;
import cn.xny.basic.util.Md5Utils;
import cn.xny.basic.util.PageList;
import cn.xny.basic.util.StrUtils;
import cn.xny.org.domain.Employee;
import cn.xny.org.mapper.EmployeeMapper;
import cn.xny.org.query.EmployeeQuery;
import cn.xny.org.service.EmployeeService;
import cn.xny.user.domain.Logininfo;
import cn.xny.user.mapper.LogininfoMapper;
import cn.xny.user.service.impl.LogininfoServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements  EmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Transactional
    @Override
    public void save(Employee employee) {
        //1.处理盐值与密码
        //加盐
        String salt = StrUtils.getComplexRandomString(32);
        //加密
        String md5pwd = Md5Utils.encrypByMd5(salt);
        employee.setSalt(salt);
        employee.setPassword(md5pwd);
        //先添加到logininfo中
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.save(logininfo);//获取自增
        //添加到employee的数据
        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);
        //添加role
        employeeMapper.addRole(employee);


    }

    @Transactional
    @Override
    public void del(Long id) {
        //先查询登录信息id
        Employee employee = employeeMapper.loadByAll(id);
        //删除logininfo
        logininfoMapper.del(employee.getLogininfo_id());
        //删除role
        employeeMapper.delByEmployeeId(id);


        //删除employee
        super.del(id);
    }

    @Transactional
    @Override
    public void update(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5pwd = Md5Utils.encrypByMd5(salt);
        employee.setSalt(salt);
        employee.setPassword(md5pwd);
        //查以前的老数据：employee里面大的数据来自于前端  -就不会有logininfo_id,shop_id
        Employee oldemployee = employeeMapper.loadByAll(employee.getId());
        //修改employee的信息
        employee.setLogininfo_id(oldemployee.getLogininfo_id());
        employee.setShop_id(oldemployee.getShop_id());

        //修改登录信息
        Logininfo oldLogininfo = logininfoMapper.loadByAll(oldemployee.getLogininfo_id());
        BeanUtils.copyProperties(employee, oldLogininfo);
        oldLogininfo.setId(oldemployee.getLogininfo_id());
        oldLogininfo.setType(0);
        oldLogininfo.setDisable(true);

        logininfoMapper.update(oldLogininfo);
        super.update(employee);
        //同步角色修改
        //先删除
        employeeMapper.delByEmployeeId(employee.getId());
        //再添加
        employeeMapper.addRole(employee);

    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    //显示店铺接单员的名字
    @Override
    public List<Employee> queryByShopId(Long shopId) {
         return employeeMapper.queryByShopId(shopId);
    }
}